package com.hxwb.robot.util;

import java.util.*;

public class RandomCountUtil {
    public static void main(String[] args) {
        //test
        System.out.println(genRandList(50,12,5,30));
    }


    private static final Random random = new Random();


    public static List<Integer> genRandList(int total, int splitNum, int min, int max) {
        return genRandList(total, splitNum, min, max, 0.95f);
    }
    //规定的总时间，该时间内订单数，返回最小值，返回最大值，精度
    public static List<Integer> genRandList(int total, int splitNum, int min, int max, float thresh) {
        assert total >= splitNum * min && total <= splitNum * max;
        assert thresh >= 0.0f && thresh <= 1.0f;
        // 平均分配
        int average = total / splitNum;
        List<Integer> list = new ArrayList<>(splitNum);
        int rest = total - average * splitNum;
        for (int i = 0; i < splitNum; i++) {
            if (i < rest) {
                list.add(average + 1);
            } else {
                list.add(average);
            }
        }
        // 如果浮动阀值为0则不进行数据随机处理
        if (thresh == 0) {
            return list;
        }
        // 根据阀值进行数据随机处理
        for (int i = 0; i < splitNum - 1; i++) {
            int nextIndex = i + 1;
            int rangeThis = Math.min(list.get(i) - min, max - list.get(i));
            int rangeNext = Math.min(list.get(nextIndex) - min, max - list.get(nextIndex));
            int rangeFinal = (int) Math.ceil(thresh * (Math.min(rangeThis, rangeNext) + 1));
            int randOfRange = random.nextInt(rangeFinal);
            int randRom = list.get(i) > list.get(nextIndex) ? -1 : 1;
            list.set(i, list.get(i) + randRom * randOfRange);
            list.set(nextIndex, list.get(nextIndex) + randRom * randOfRange * -1);
        }
        System.out.println("数据："+list);
        return list;
    }


}
